package com.bojiu.lawsearch.lawsearch.service.impl;

import com.bojiu.lawsearch.lawsearch.mapper.WechatUserMapper;
import com.bojiu.lawsearch.lawsearch.mapper.WithdrawMapper;
import com.bojiu.lawsearch.lawsearch.model.Page;
import com.bojiu.lawsearch.lawsearch.model.Task;
import com.bojiu.lawsearch.lawsearch.model.WechatUser;
import com.bojiu.lawsearch.lawsearch.model.Withdraw;
import com.bojiu.lawsearch.lawsearch.service.WithdrawService;
import com.bojiu.lawsearch.lawsearch.util.JsonResult;
import com.bojiu.lawsearch.lawsearch.util.UtilTools;
import com.bojiu.lawsearch.lawsearch.wxzf.WXZFService;
import com.bojiu.lawsearch.lawsearch.wxzf.WXZFUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author: afdiao
 * @Date：2020/4/8 21:58
 * @Description:
 */
@Service
public class WithdrawServiceImpl implements WithdrawService {

    @Autowired
    WechatUserMapper wechatUserMapper;

    @Autowired
    private WithdrawMapper withdrawMapper;

    @Autowired
    private WXZFService wxzfService;

    @Override
    public void apply(Integer userId, BigDecimal amount) {
        WechatUser wechatUser = (WechatUser) wechatUserMapper.selectOneByExample(UtilTools.createExampleByKey("id", userId, WechatUser.class));
        Withdraw withdraw = new Withdraw();
        withdraw.setWithdrawAccount(wechatUser.getOpenid());
        withdraw.setAmount(amount);
        withdraw.setUserId(userId);
        withdraw.setState(0);
        withdrawMapper.insertSelective(withdraw);
    }

    @Override
    public JsonResult toWx(Integer withdrawId, String remark) {
        Withdraw withdraw = (Withdraw) withdrawMapper.selectOneByExample(UtilTools.createExampleByKey("id", withdrawId, Withdraw.class));
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        JsonResult jsonResult = wxzfService.transfers(withdraw.getWithdrawAccount(), "tx" + withdrawId.toString(), withdraw.getAmount().multiply(new BigDecimal("100")).intValue(), "提现", WXZFUtil.getIpAddress(request));
        if ("200".equals(jsonResult.getCode())) {
            withdraw.setState(1);
            withdraw.setRemark(remark);
            Gson gson = new Gson();
            JsonObject jsonObject = gson.fromJson(gson.toJson(jsonResult.getData()), JsonObject.class);
            withdraw.setPaymentNo(jsonObject.get("payment_no").getAsString());
            withdraw.setPaymentTime(jsonObject.get("payment_time").getAsString());
        } else {
            withdraw.setState(2);
            withdraw.setRemark("接口异常");
        }
        withdrawMapper.updateByExampleSelective(withdraw, UtilTools.createExampleByKey("id", withdrawId, Withdraw.class));
        return jsonResult;
    }

    @Override
    public void reject(Integer withdrawId, String remark) {
        Withdraw withdraw = new Withdraw();
        withdraw.setState(2);
        withdrawMapper.updateByExampleSelective(withdraw, UtilTools.createExampleByKey("id", withdrawId, Withdraw.class));
    }

    @Override
    public Page<Withdraw> queryPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize, true);
        List<Withdraw> list = withdrawMapper.queryPage();
        PageInfo pageInfo = new PageInfo(list);
        Page page = new Page(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getPages(), pageInfo.getTotal(), list);
        return page;
    }
}
